package lxs.swift.instance.action;


import lxs.swift.instance.StructInstance;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;

public class StringAction<Key> extends Action<Key, String> {

    public StringAction(StructInstance<Key> instance, Function<Object, String> convertor) {
        super(instance, convertor);
    }

    @SafeVarargs
    public final String join(String delimiter, Key... keys) {
        Objects.requireNonNull(delimiter);
        Objects.requireNonNull(keys);
        List<String> values = values(keys);
        return String.join(delimiter, values);
    }

    @SafeVarargs
    public final String joinConcat(String prefix, String suffix, String delimiter, Key... keys) {
        Objects.requireNonNull(prefix);
        Objects.requireNonNull(suffix);
        return prefix + this.join(delimiter, keys) + suffix;
    }

    public boolean isEmpty(Key key) {
        String value = get(key);
        return value == null || value.isEmpty();
    }

    public boolean isContains(Key key, String value) {
        String v = get(key);
        if (v == null) return value == null;
        if (v.isEmpty()) return value.isEmpty();
        return v.contains(value);
    }

    public boolean isMatch(Key key, String regex) {
        String v = get(key);
        if (v == null) return regex == null;
        if (v.isEmpty()) return regex.isEmpty();
        return v.matches(regex);
    }

    public boolean isStartWith(Key key, String prefix) {
        String v = get(key);
        if (v == null) return prefix == null;
        if (v.isEmpty()) return prefix.isEmpty();
        return v.startsWith(prefix);
    }

    public boolean isEndWith(Key key, String suffix) {
        String v = get(key);
        if (v == null) return suffix == null;
        if (v.isEmpty()) return suffix.isEmpty();
        return v.endsWith(suffix);
    }
}
